package com.tron.auth.jwttest;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.FileUrlResource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.io.File;
import java.net.MalformedURLException;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.util.HashMap;

public class Start {
    public static void main(String[] args) throws MalformedURLException {
        String keyStore = "auth-server/src/main/java/com/tron/auth/jwttest/tron.keystore";
        String StorePass = "zhenwuhuan";
        String alias = "tronkey";
        String keyPass = "zhenwuhuan";
        FileUrlResource resource = new FileUrlResource(keyStore);
        //获取令牌工厂
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, StorePass.toCharArray());
        //获取密钥对
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair(alias, keyPass.toCharArray());
        //获取RSA私钥
        RSAPrivateKey key = (RSAPrivateKey) keyPair.getPrivate();
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "tron");
        String tronJson = JSON.toJSONString(map);
        //生成jwt令牌
        Jwt jwt = JwtHelper.encode(tronJson, new RsaSigner(key));
        //获取jwt令牌字符串
        String jwtEncoded = jwt.getEncoded();
        //eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidHJvbiJ9.XxcKuCawQ3UCYddSXCYH1iiGhxu75z41O4NkeAv9JGPSzzGKFmI7FFPsumSjGzO4LwQdqLROekmO8nvY67HKCuWdIiZmuS3ZR0396qsl7_TFZR8K2nCsha2zXkTssTYUx1josVI1nQ4NouoELDlvLP3P6RXvRfgHx5HjP56ippLCRSxqHaY4OBqemA4VBbflkX2TsH8UmA-aqZNslQHEUXOGb76lOThTpbZcEe-BA2MqdQ1gobysOiMOLP0m969ca578hGvW9GLbnv1SIqmMfHgXaohCVQToi8r_Yp6hzAMMxaYPM2cEmjI6ulucj9Ub4v3RxRyqvFsyG0pY8O_7CA
        System.out.println(jwtEncoded);

        //校验jwt令牌

        //jwt令牌
        String jwtString = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidHJvbiJ9.XxcKuCawQ3UCYddSXCYH1iiGhxu75z41O4NkeAv9JGPSzzGKFmI7FFPsumSjGzO4LwQdqLROekmO8nvY67HKCuWdIiZmuS3ZR0396qsl7_TFZR8K2nCsha2zXkTssTYUx1josVI1nQ4NouoELDlvLP3P6RXvRfgHx5HjP56ippLCRSxqHaY4OBqemA4VBbflkX2TsH8UmA-aqZNslQHEUXOGb76lOThTpbZcEe-BA2MqdQ1gobysOiMOLP0m969ca578hGvW9GLbnv1SIqmMfHgXaohCVQToi8r_Yp6hzAMMxaYPM2cEmjI6ulucj9Ub4v3RxRyqvFsyG0pY8O_7CA";
        //公钥
        String publicKey = "-----BEGIN PUBLIC KEY-----\n" +
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAobYyXyjlLUrNhHhHLiq8\n" +
                "CG8pLywRoV+GJSjruJ9FvwTkvWXTOmaWQp67Z7KfqSmNL9ohRF+CbV9MJol23oQ/\n" +
                "OVncQHD7X8QFKmdDHO2LacX6pSsHOpOjNPyCzEMMIPcfwrtz3yaOeInSw9fOvHf8\n" +
                "Up+GaQDtE70xSWJ6+UVWE5F2eHybFG+wY2L9IoNPqmfjMChcEp50fdkbz71/94FS\n" +
                "NwrQxsJt7Xs92KRIhUIdcMm2fuxqo+GgDxFTBUeLH469j8Ny9w13P3o1dieSRQAD\n" +
                "7MdjalkrvkEjffh9tRcrFF+kFL+ta8RY0BqO2Ozgtqp60e3PcHfq8T+oh23ft+Pj\n" +
                "1QIDAQAB\n" +
                "-----END PUBLIC KEY-----";
        //校验
        Jwt verify = JwtHelper.decodeAndVerify(jwtString, new RsaVerifier(publicKey));
        //获取令牌内容
        String claims = verify.getClaims();
        System.out.println(claims);
    }
}
